package com.winter.config;

import com.winter.shrio.CustomRealm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;

import javax.security.auth.message.config.AuthConfig;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chengang
 */
@Configuration
public class ShiroConfig {

    @Bean
    public SessionDAO customSessionDAO() {
        return new CustomSessionDAO();
    }

    @Bean
    public SessionManager customSessionManager() {
        CustomSessionManager sessionManager = new CustomSessionManager();

        Cookie cookie = new SimpleCookie("SNMT.SESSION.ID");

        sessionManager.setGlobalSessionTimeout(30 * 60 * 1000);
        sessionManager.setSessionValidationInterval(60 * 60 * 1000);
        sessionManager.setSessionValidationSchedulerEnabled(Boolean.TRUE);
        sessionManager.setSessionIdCookieEnabled(Boolean.TRUE);
        sessionManager.setDeleteInvalidSessions(Boolean.TRUE);
        sessionManager.setSessionIdCookie(cookie);

        // 自定义SessionDAO
        sessionManager.setSessionDAO(customSessionDAO());

        return sessionManager;
    }
    @Bean
    public CustomRealm customRealm() {
        CustomRealm customRealm = new CustomRealm();
        return customRealm;
    }

    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(customRealm());
//        CustomSessionManager sm = new CustomSessionManager();//把session交给shiro管理
        defaultSecurityManager.setSessionManager(customSessionManager());
        return defaultSecurityManager;
    }


    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(securityManager);

        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/success");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized.jsp");

        // filter
//        Map<String, Filter> filters = new HashMap<>();
//        FormAuthenticationFilter authenticationFilter = new FormAuthenticationFilter();
//        filters.put("authc", authenticationFilter);
//        shiroFilterFactoryBean.setFilters(filters);

        Map<String,String> filterChainMap = new LinkedHashMap<>();
        filterChainMap.put("/*.jsp", "anon");
        filterChainMap.put("/login", "anon");
        filterChainMap.put("/register", "anon");


        filterChainMap.put("/**", "authc");
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");
        // filterChainDefinition
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);

        return shiroFilterFactoryBean;
    }

}
